<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<div id="app">
    <template>

        <el-card style="background-color: #f7f7f7">
            <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="100px">

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-row style="margin-top: 1%">
            <el-col :span="1.5">


                <el-upload
                        class="upload-demo"
                        action="/workflow/definition/upload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        :on-success="handleSuccess"
                        multiple
                        :limit="3"
                        :on-exceed="handleExceed"
                        :file-list="fileList">
                    <el-button
                            type="primary"
                            icon="el-icon-plus"
                            size="mini"
                    >
                        部署流程定义
                    </el-button>
                </el-upload>

            </el-col>
        </el-row>


        <el-table
                v-loading="loading"

                :data="tableData" row-key="id"
                :header-cell-style="{'text-align':'center','color':'#303133','font-weight':'600','background-color': '#F5F7FA'}"
                :cell-style="{'text-align':'center'}" border style="margin-top: 10px"
        >
            <el-table-column label="流程ID" align="center" prop="id"></el-table-column>
            <el-table-column label="部署ID" align="center" prop="deploymentId"></el-table-column>
            <el-table-column label="流程KEY" align="center" prop="key"></el-table-column>
            <!--            <el-table-column label="流程名称" align="center" prop="name"></el-table-column>-->
            <el-table-column label="版本" align="center" prop="version"></el-table-column>
            <!--            <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip="true"></el-table-column>-->
            <el-table-column label="流程定义状态" align="center" prop="suspendState">
                <template slot-scope="scope">
                    <el-tag
                            effect="dark" type="success" v-if="scope.row.suspendState==='1'">
                        已激活
                    </el-tag>
                    <el-tag
                            effect="dark" type="danger" v-else>
                        已挂起
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="部署时间" align="center" prop="deploymentTime">
                <template slot-scope="scope">
                    <span>{{ scope.row.deploymentTime | timeFilter}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="400">
                <template slot-scope="scope">

                    <el-button size="mini" type="primary" icon="el-icon-phone"
                               @click="handout(scope.row,'挂起')" v-if="scope.row.suspendState==='1'">挂起
                    </el-button>
                    <el-button size="mini" type="success" icon="el-icon-s-promotion" v-else
                               @click="handout(scope.row,'激活')">激活
                    </el-button>

                    <el-button size="mini" type="warning" icon="el-icon-s-unfold"
                               @click="viewProcess(scope.row)">流程图
                    </el-button>

                    <el-button size="mini" type="primary" icon="el-icon-s-unfold"
                               @click="convertModel(scope.row)">转模型
                    </el-button>

                    <el-button size="mini" type="info" icon="el-icon-delete"
                               @click="deleteDefinition(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>

        </el-table>
        <el-pagination
                background
                :page-sizes="[10, 20, 30, 40,50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :page-size.sync="pageSize"
                :current-page.sync="currentPage"
                @size-change="handleQuery"
                @current-change="handleQuery"
                style="float: right;margin-top: 10px"
        />
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/workflow/processDefinition.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    Vue.filter("timeFilter", function (timestamp) {
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = ("0" + (date.getMonth() + 1)).slice(-2);
        const day = ("0" + date.getDate()).slice(-2);
        const hours = ("0" + date.getHours()).slice(-2);
        const minutes = ("0" + date.getMinutes()).slice(-2);
        const seconds = ("0" + date.getSeconds()).slice(-2);
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

    });
    new Vue({
        el: '#app',
        data() {
            return {
                fileList: [],
                currentPage: 1,
                pageSize: 10,
                pageNo: 1,
                total: 0,
                tableData: [],

                button: {
                    add: false,
                    update: false,
                    delete: false
                },
                loading: false,
                queryForm: {}
            }
        },
        created() {
            this.handleQuery();
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("admin/sysConfig/add") > -1;
            this.button.update = sysButtons.indexOf("admin/sysConfig/update") > -1;
            this.button.delete = sysButtons.indexOf("admin/sysConfig/delete") > -1;

        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`);
            },
            handleSuccess(res, file) {
                this.$notify({
                    title: '成功',
                    message: '部署成功',
                    type: 'success'
                });
                this.handleQuery()
            },
            deployDefinition() {


            },
            viewProcess(row) {
                requests.post("/workflow/definition/getProcessImg", {deploymentId: row.deploymentId})
                    .then((res) => {
                        if (res.code === '200') {
                            openMiddlePage('流程图', '/workflow/processImg.html?img=' + res.data, this)
                        }
                    });
                // console.log(row)
                // requests.post("/workflow/node/getNodeList", {deploymentId: row.deploymentId, key: row.key})
                //     .then((res) => {
                //         console.log(res)
                //     });
            },
            handout(row, name) {
                let params = {}
                params.id = row.id
                params.suspendState = row.suspendState
                submitConfirm("/workflow/definition/suspendOrActiveApply", name, params, this)

            },
            convertModel(row) {
                let params = {}
                params.id = row.id
                submitConfirm("/workflow/definition/convertToModel", "转模型", params, this)
            },
            deleteDefinition(row) {
                let params = {}
                params.deploymentId = row.deploymentId
                submitConfirm("/workflow/definition/deleteProcessDeploymentByIds", "删除流程定义", params, this)
            },
            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            handleQuery() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage;
                queryParam.pageSize = this.pageSize;
                this.loading = true
                pageTableData("/workflow/definition/list", queryParam, this)
            },
            handleDelete(row) {
                var that = this;
                parent.layer.confirm('<font color="var(--baseBackground)">确定删除该数据吗?</font>', {
                    icon: 3,
                    title: "删除数据操作",
                    shade: [0.3, '#000'],
                    shadeClose: true,
                    id: "one",
                    anim: 1,
                    move: true,
                    skin: "my-skin",
                    btn: ['确定', '取消']
                }, function (index) {
                    requests.post("/workflow/modeler/remove", {ids: row.id})
                        .then((res) => {
                            if (res.code === '200') {
                                parent.layer.msg('删除成功');
                                parent.layer.close(index);
                                that.handleQuery();
                            }
                        });
                });

            },

            formatDate(timestamp) {
                const date = new Date(timestamp);
                const year = date.getFullYear();
                const month = ("0" + (date.getMonth() + 1)).slice(-2);
                const day = ("0" + date.getDate()).slice(-2);
                const hours = ("0" + date.getHours()).slice(-2);
                const minutes = ("0" + date.getMinutes()).slice(-2);
                const seconds = ("0" + date.getSeconds()).slice(-2);
                return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            },

            handleAddOrUpdate(type, row) {
                if (type === 1) {
                    openPage('创建模型', '/workflow/modelAddUpdate.html', this);
                } else {
                    openFullPage('绘制流程', '/modeler/modeler.html?modelId=' + row.id, this);
                }
            },
        }
    });
</script>
</html>
